var express = require('express');
var router = express.Router();
const db = require("../database/db");

// 全部留言
router.post('/all', function (req, res, next) {
    let textid = req.body.textid;
    let sql = `SELECT * FROM liuyan WHERE  textid=?`;
         db.query(sql, [textid], function (err, result) {
           if (err) {
             console.log(err);
             res.json({ code: 400, msg: "查询失败", data: err });
           } else {

             console.log(result);

             res.json({ code: 200, msg: "查询成功", data: result });
           }
         });
})

//添加留言
router.post('/add', function (req, res, next) {
    console.log(req.body);
    
    let {textid, nei} = req.body;
    if (!textid || !nei) {
        res.send({
            code: 400,
            msg: '参数错误'
        })
        return;
    }
const sql = `INSERT INTO liuyan (textid, nei) VALUES (?, ?)`;
db.query(sql, [textid, nei], function (err, result) {
  if (err) {
    res.json({
      code: 400,
      msg: "添加错误",
    });
    return;
  }
  res.json({
    code: 200,
    msg: "添加成功",
    data: result,
  });
});
})

// 删除留言
router.post('/del', function (req, res, next) {
    
})











module.exports = router;